package com.qcs.sleep;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qcs.sleep.service.SleepListenerServicer;

import java.text.SimpleDateFormat;

public class SplashActivity extends BaseActivity {
    private SystemTimeReceiver systemTimeReceiver;
    private TextView tipDateTime;

    private SimpleDateFormat dateFormat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_lay);

        tipDateTime = (TextView) findViewById(R.id.tipTime);

        Typeface textFont = Typeface.createFromAsset(getAssets(), "fonts/_roboto_thin.ttf");
        tipDateTime.setTypeface(textFont);

        if (null == dateFormat)
            dateFormat = new SimpleDateFormat("HH:mm");

        systemTimeReceiver = new SystemTimeReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();

        tipDateTime.setText(dateFormat.format(System.currentTimeMillis()));

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);
        registerReceiver(systemTimeReceiver, intentFilter);

        Intent t = new Intent();
        t.setClass(this, SleepListenerServicer.class);

        startService(t);
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(systemTimeReceiver);
    }

    @Override
    public void onBackPressed() {
    }

    class SystemTimeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tipDateTime.setText(dateFormat.format(System.currentTimeMillis()));
        }
    }

    //  关闭屏幕
    private void closeScence() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.screenBrightness = 0;
        getWindow().setAttributes(params);
    }

}